package com.icode.dao.impl;

import com.icode.core.model.house.HouseBaseArea;
import com.icode.core.model.house.HouseInfo;
import com.icode.core.model.house.HouseWeek;
import com.icode.core.model.house.HouseWeekTime;
import com.icode.core.model.manage.Reserve;
import com.icode.dao.HouseWeekDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-24
 * Time: 下午5:27
 * To change this template use File | Settings | File Templates.
 */
@Repository("houseWeekDao")
public class HouseWeekDaoImpl extends EntityDaoImpl implements HouseWeekDao {


    @Override
    public List<HouseInfo> findHouseInfoByArea(String area) {
        String hql = "from " + HouseInfo.class.getName() + " a where a.buildingType.houseBase.houseBaseArea=:area";
        Query query = currentSession().createQuery(hql);
        query.setParameter("area", HouseBaseArea.valueOf(area));
        return query.list();
    }

    @Override
    public List<HouseWeek> findHouseWeekByArea(String area) {
        String hql = "from " + HouseWeek.class.getName() + " a where a.houseBaseArea=:area";
        Query query = currentSession().createQuery(hql);
        query.setParameter("area", HouseBaseArea.valueOf(area));
        return query.list();
    }

    @Override
    public HouseWeek findHouseWeekByWeek(String week, HouseInfo houseInfo) {
        String hql = "from " + HouseWeek.class.getName() + " a where a.name=:week and a.houseInfo=:houseInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("week", week);
        query.setParameter("houseInfo", houseInfo);
        List<HouseWeek> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void deleteObject(Object object) {
        currentSession().delete(object);
    }

    @Override
    public List<HouseWeek> findHouseWeekType(String area, HouseInfo houseInfo) {
        String hql = "from " + HouseWeek.class.getName() + " a where a.houseBaseArea=:area and a.houseInfo=:houseInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("area", HouseBaseArea.valueOf(area));
        query.setParameter("houseInfo", houseInfo);
        return query.list();
    }

    @Override
    public List<HouseWeekTime> findHouseWeekTimeByArea(String area) {
        String hql = "from " + HouseWeekTime.class.getName() + " a where a.houseWeek.houseBaseArea=:area";
        Query query = currentSession().createQuery(hql);
        query.setParameter("area", HouseBaseArea.valueOf(area));
        return query.list();
    }

    @Override
    public void updateStateForHouseWeekTimeByGuids(List<String> weekTimeGuids) {
        String hql = "update " + HouseWeekTime.class.getName() + " h set h.status=:status where h.guid in (:weekTimeGuids)";
        Query query = currentSession().createQuery(hql);
        query.setParameter("weekTimeGuids", weekTimeGuids);
        query.setParameter("status", 0);
        query.executeUpdate();
    }

    @Override
    public void deleteHouseWeekTimeByHouseWeek(HouseWeek houseWeek) {
        String hql = "delete " + HouseWeekTime.class.getName() + " h where h.houseWeek=:houseWeek";
        Query query = currentSession().createQuery(hql);
        query.setParameter("houseWeek", houseWeek);
        query.executeUpdate();

    }

    @Override
    public List<HouseWeek> findHouseWeekByHouseInfoGuid(String guid) {
        String hql = "from " + HouseWeek.class.getName() + " a where a.houseInfo.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("guid", guid);
        return query.list();
    }

    @Override
    public Reserve findReserveByHouseInfoGuid(String houseInfoGuid) {
        String hql = "from " + Reserve.class.getName() + " a where a.houseInfo.guid=:guid ";
        Query query = currentSession().createQuery(hql);
        query.setParameter("guid", houseInfoGuid);
        List<Reserve> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }
}
